﻿package kliment.controls.mediaplayer {
	
	/**
	 * ...
	 * @author	Kliment
	 * @version	1.1.2
	 * 
	 * class name: kliment.controls.mediaplayer.MediaPlayerPlaylist
	 */
	public class MediaPlayerPlaylist {
		private var _list:Array;
		private var _currentIndex:int = -1;
		
		public function MediaPlayerPlaylist() {
			_list = new Array();
			
		}
		
		public function add(value:MediaPlayerPlaylistItem):uint {
			if (_currentIndex < 0)
				_currentIndex = 0;
			return _list.push(value);
		}
		
		public function remove(index:int):Boolean {
			if (0 > index && index > _list.length) 
				return false;
			_list.splice(index, 1);
			if (_currentIndex > index)
				_currentIndex--;
			return true;
		}
		
		public function get first():MediaPlayerPlaylistItem {
			_currentIndex = 0;
			return _list[_currentIndex];
		}
		
		public function get last():MediaPlayerPlaylistItem {
			_currentIndex = _list.length-1;
			return _list[_currentIndex];
		}
		
		public function get next():MediaPlayerPlaylistItem {
			_currentIndex += 1;
			if (_list[_currentIndex]) {
				return _list[_currentIndex];
			}
			_currentIndex -= 1;
			return null;
		}
		
		public function get prev():MediaPlayerPlaylistItem {
			_currentIndex -= 1;
			if (_list[_currentIndex]) {
				return _list[_currentIndex];
			}
			_currentIndex += 1;
			return null;
		}
		
		public function get current():MediaPlayerPlaylistItem {
			return _list[_currentIndex];
		}
		
		public function set currentIndex(index:int):void {
			if (0 > index && index > _list.length) 
				return;
			_currentIndex = index;
		}
		public function get currentIndex():int {
			return _currentIndex;
		}
		
		public function get length():int {
			return _list.length;
		}
		
		public function toString(key:String = "path"):String {
			var str:String = '';
			for (var i:int = 0; i < _list.length; i++){
				str += _list[i][key]+', ';
			}
			str = str.substring(0, str.length-2);
			return "PlayList: [ " + str + " ]";
		}
		
	}

}